package cn.xkai.exercise.b;

import com.google.gson.Gson;

import java.util.HashSet;

/**
 * @description: 两个数组的交集
 * 自己的思路：把数组数据放到集合中，然后判断是否重复的值
 * @author: kaixiang
 * @date: 2022-07-26
 **/
public class Solution97 {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set1 = new HashSet<>();
        HashSet<Integer> set2 = new HashSet<>();

        for (int i : nums1) {
            set1.add(i);
        }

        for (int i : nums2) {
            if (set1.contains(i)) {
                set2.add(i);
            }
        }

        int[] res = new int[set2.size()];
        int index = 0;
        for (Integer num : set2) {
            res[index++] = num;
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums1 = {1, 2, 2, 1};
        int[] nums2 = {2, 2};
        Solution97 solution97 = new Solution97();
        System.out.println(new Gson().toJson(solution97.intersection(nums1, nums2)));
    }
}
